package org.testis.communicationmodule.services;

import org.testis.communication.client.AtofTelnetClient;
import org.testis.communication.data.CommunicationModuleDataObject;
import org.testis.data.TestStepException;
import org.testis.data.TestStepResultType;
import org.testis.logging.AtofLogLevel;
import org.testis.logging.LogSenderService;
import org.testis.services.AtofModuleAction;
import org.testis.services.AtofModuleService;
import org.testis.utils.StringVerification;
import java.io.IOException;

/**
 * A class to define an telnet client action for
 * Communication module
 * @author nurhat
 */
public class TelnetClientAction extends AtofModuleAction {

    /**
     * Function connects to given host and send and send a command to telnet client
     * by using AtofTelnetClient
     * @param request
     * @return
     */
    @Override
    public boolean operateRequest(AtofModuleService request) {
        CommunicationModuleDataObject dataObj =
                (CommunicationModuleDataObject) request.getDataObj();
        LogSenderService.putMessageOnQueue(
                "TelnetClientAction",
                AtofLogLevel.INFO,
                "Starting to execute telnet client action ....with data:" + dataObj.toString() + "....",
                dataObj.getMessageId());

        boolean result = false;
        String userName = dataObj.getUserName(),
                passWord = dataObj.getPassword();

        AtofTelnetClient telnet = new AtofTelnetClient(dataObj.getHost(),
                userName,
                passWord);
        telnet.setCommandPromptChar(
                dataObj.getCommandPromptCharForTelnetClient());
        telnet.connect();

        if (telnet.isConnected()) {
            LogSenderService.putMessageOnQueue(
                    "TelnetClientAction",
                    AtofLogLevel.INFO,
                    "Connected to  telnet client...",
                    dataObj.getMessageId());
            try {
                if (telnet.login()) {
                    LogSenderService.putMessageOnQueue(
                            "TelnetClientAction",
                            AtofLogLevel.INFO,
                            "Logged into  telnet client...",
                            dataObj.getMessageId());
                    LogSenderService.putMessageOnQueue(
                            "TelnetClientAction",
                            AtofLogLevel.INFO,
                            "Sending command " + dataObj.getCommandToSend()
                            + " to  telnet client...",
                            dataObj.getMessageId());
                    String output = telnet.sendCommand(
                            dataObj.getCommandToSend());
                    String verify = "";
                    if (dataObj.getStringToSendAndVerify() != null) {
                        verify = dataObj.getStringToSendAndVerify().trim();
                    }

                    if (!verify.equals("")) {
                        result = StringVerification.verifyText(verify,
                                output);
                    } else {
                        result = true;
                    }
                    LogSenderService.putMessageOnQueue(
                            "TelnetClientAction",
                            AtofLogLevel.INFO,
                            "Output of command " + dataObj.getCommandToSend()
                            + " is:" + output,
                            dataObj.getMessageId());
                    telnet.disconnect();
                } else {
                    telnet.disconnect();
                    throw new TestStepException(TestStepResultType.ERROR,
                            "Could not login to  telnet client...");
                }
            } catch (NullPointerException ex) {
                throw new TestStepException(TestStepResultType.ERROR,
                        "Execution of telnet client failed due to NullPointerException..."
                        + ex.toString());
            } catch (IOException ex) {
                throw new TestStepException(TestStepResultType.ERROR,
                        "Execution of telnet client failed due to IOException..."
                        + ex.toString());
            } catch (Exception ex) {
                throw new TestStepException(TestStepResultType.ERROR,
                        "Execution of telnet client failed due to Exception..."
                        + ex.toString());
            }

        } else {
            throw new TestStepException(TestStepResultType.ERROR,
                    "Could not connect to  telnet client...");

        }
        AtofLogLevel level = result ? AtofLogLevel.INFO : AtofLogLevel.ERROR;
        LogSenderService.putMessageOnQueue(
                "TelnetClientAction",
                level,
                "TelnetClientAction executed " + (result ? "" : "un")
                + "successfully...",
                dataObj.getMessageId());
        return result;
    }
}
